#include <Starflub/Starflub.h>

#define ever_and_ever_and_ever ;;

u32int initial_esp;

int x=0, y=0;
int lastx=0, lasty=0;
//mouse.c
extern int mouse_x, mouse_y;

//callback for mouse calculation
//this is all Anshu, Michael can't claim credit for it.
void mousetmrcb(registers_t regs) {
	if(lastx!=mouse_x || lasty!=mouse_y) {
//		monitor_write("X:");
//		monitor_write_dec(mouse_x);
//		monitor_write(", Y:");
//		monitor_write_dec(mouse_y);
		lastx = mouse_x;
		lasty = mouse_y;
		
		/* WARNING: This is the mother of all ugly things in the world */
		//don't proceed if you're not at least in an honors college-preparatory algebra class.
		
		//127.....255/0.....127
		//becomes
		//-127.....0.....127
		if(mouse_x>137) //first half, 127->255
			//convert to -127->0, scale down
			x += (-.125f*(255-mouse_x+127))/(127/16);
		else if(mouse_x < 117)
			x += (mouse_x*2.0f)/(127/16);
			
		if(mouse_y<117)//for some reason the y-axis is flipped
			y += (-.125f*(255-mouse_y+127))/(127/2);
		else if(mouse_y > 137);
			y += (mouse_y)/(127);

		if(x>79)x=79;
		if(y>24)y=24;
		if(x<0)x=0;
		if(y<0)y=0;
//		monitor_write(";  x:");
//		monitor_write_dec(x);
//		monitor_write(", y:");
//		monitor_write_dec(y);
//		monitor_write("\r\n");
		draw_mouse_at(x,y);
		/*
			OK, I struggled with everything you see above you for several hours, so					127
			I'm going to explain it to show off my math skillz.											 ^
			Basically, the mouse driver is weird. Really weird. It gives an x,y coord			255 | 0		i think this is the y-axis.
			which is actually representative of the speed at which the mouse is						 v
			moving.																										127
			127 <--- 255  |  0 ---> 127
			Above is the x-axis. It's split in two parts: forward and reverse. As you can
			see, forward is 0->127, 0 being slow, 127 being fast. Reverse is 255->127.
			Obviously, weird.
			So what I did was to flip the left, cut it in half, *-1 to get this:
			-127 <--- 0 ---> 127
			Better right? There was also a lot of scaling to do to slow it down
			enough (hence the divide by 127/16, the multiply by .125f, etc).
			It's also noteworthy that it's not really 0->127, but some strange number like 192
			This number is pretty much the limit of human hands.
			In actuality, you have something like:
			0 <--- 255 | 0 ---> 255
			But you have to move your hand at breakneck speed to get that fast. And it makes
			the math a lot harder.
		*/
	}
}

int main(struct multiboot *mboot_ptr, u32int initial_stack) {
	SFInit(mboot_ptr);
	initial_esp = initial_stack;
	
	//for mouse things
	register_interrupt_handler(IRQ0, &mousetmrcb);
	u32int divisor = 1193180 / 50;
	outb (0x43, 0x36);
	u8int l = (u8int) (divisor & 0xFF);
	u8int h = (u8int) ( (divisor >> 8) & 0xFF);
	outb(0x40, l);
	outb(0x40, h);
	SFSysLog("Mouse timer started.\r\n\r\n");
	
	// Change nothing above here unless you know what you're doing.	
	/////// YOUR CODE GOES AFTER HERE
	
	SFMsgBox("I'm in ur msgbox","You are testing mouse");
	
	/////// OK STOP CODING
	// Change nothing below here unless you know what you're doing.
	
	for(ever_and_ever_and_ever); //lol
	return 0;
}

